﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Admin_Article_FuJian : BasePage
{
    protected int articleID;
    protected void Page_Load(object sender, EventArgs e)
    {
        Delete();
        if(!int.TryParse(Request.QueryString["articleid"],out articleID))
            return;
        if (!IsPostBack)
        {
            
            BindData();
        }
    }
    protected void BindData()
    {
        PageInfo.Where = "where articleid="+articleID.ToString();
        gv.DataSource = new MatchIns.BLL.TFuJian().GetPageList(PageInfo).List;
        gv.DataBind();
    }

    protected void Delete()
    {
        string fujianid = Request.QueryString["fujianid"];
        if (fujianid != null)
        {
            MatchIns.BLL.TFuJian bll = new MatchIns.BLL.TFuJian();
            MatchIns.Model.TFuJian model = bll.GetModel(int.Parse(fujianid));
            if (model == null)
                return;
            if (System.IO.File.Exists(Server.MapPath("~/uploadfile/fujian/"+model.FileName)))
            {
                System.IO.File.Delete(Server.MapPath("~/uploadfile/fujian/" + model.FileName));
            }
            bll.Delete(model.FuJianID);
        }
    }
    protected void save_Click(object sender, EventArgs e)
    {
        errors.Items.Clear();
        MatchIns.Model.TFuJian aFuJian = new MatchIns.Model.TFuJian();
        aFuJian.ArticleID=articleID;
        aFuJian.Title=tbName.Text;
        if(aFuJian.Title=="")
            errors.Items.Add("填写附件名称");
        if(!upload.HasFile)
            errors.Items.Add("选择附件");
        if(errors.Items.Count>0)
            return;
        string fileName= DateTime.Now.ToString("yyyyMMddHHmmssff")+System.IO.Path.GetExtension(upload.FileName);
        aFuJian.FileName=fileName;
        upload.SaveAs(Server.MapPath("~/UploadFile/FuJian/"+fileName));
        new MatchIns.BLL.TFuJian().Add(aFuJian);
        errors.Items.Add("附件:"+aFuJian.Title+",添加成功.");
        BindData();
    }
    protected void cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ArticleList.aspx");
    }
}
